<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从一个图片文件中读取 EXIF 头信息</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.exif-imagetype.html">« exif_imagetype</a></li>
      <li style="float: right;"><a href="function.exif-tagname.html">exif_tagname »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.exif.html">Exif 函数</a></li>
    <li>从一个图片文件中读取 EXIF 头信息</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.exif-read-data" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">exif_read_data</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.2.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">exif_read_data</span> &mdash; <span class="dc-title">从一个图片文件中读取 <abbr title="Exchangeable Image File ">EXIF</abbr> 头信息</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.exif-read-data-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>exif_read_data</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type">resource</span>|<span class="type">string</span></span> <code class="parameter">$file</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$required_sections</code><span class="initializer"> = <strong><code>null</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">bool</span> <code class="parameter">$as_arrays</code><span class="initializer"> = <strong><code>false</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">bool</span> <code class="parameter">$read_thumbnail</code><span class="initializer"> = <strong><code>false</code></strong></span></span><br>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>exif_read_data()</strong></span> 函数从一个图片文件中读取
   <abbr title="Exchangeable Image File ">EXIF</abbr> 头信息。这样就可以读取数码相机产生的元数据。
  </p>
  <p class="para">
   <abbr title="Exchangeable Image File ">EXIF</abbr> 头信息往往存在于数码相机生成的 JPEG/TIFF
   图片中，但不幸的是每个数码相机制造商的标记都不同，因此（编写代码时）不能依赖于某个特定的
   Exif 头信息。
  </p>
  <p class="para">
   <code class="literal">Height</code> 和 <code class="literal">Width</code> 是用和 <span class="function"><a href="function.getimagesize.html" class="function">getimagesize()</a></span>
   一样的方法计算的，因此它们的值不能是任何返回的头信息的部分。此外
   <code class="literal">html</code> 是一个可以用于普通的 <abbr title="Hyper Text Markup Language">HTML</abbr> 中的 height/width 的文本字符串。
  </p>
  <p class="para">
   当一个 Exif 头信息包含有一个 Copyright 时注意它本身可以包含两个值。解决方案和
   Exif 2.10 标准不一致，<code class="literal">COMPUTED</code> 区段会同时返回
   <code class="literal">Copyright.Photographer</code> 和
   <code class="literal">Copyright.Editor</code>，但是 <code class="literal">IFD0</code>
   区段则包含有一个字节数组用 NULL 字符分隔开两个项目。或者只有第一项如果数据类型错误的话（Exif
   的正常行为）。<code class="literal">COMPUTED</code> 也会包含
   <code class="literal">Copyright</code>，要么是原始的版权字符串，要么是逗号分隔的摄像与编辑的版权信息。
  </p>
  <p class="para">
   <code class="literal">UserComment</code> 标记和 Copyright
   有同样的问题。它也可以存储两个值，第一个是使用的编码方式，第二个是其值本身。如果这样则
   <code class="literal">IFD</code> 区段仅包含编码方式或者一个字节数组。<code class="literal">COMPUTED</code>
   区段将存储两个值到 <code class="literal">UserCommentEncoding</code> 和
   <code class="literal">UserComment</code>。<code class="literal">UserComment</code>
   在两种情况下都可用因此应该优先使用它而不是 <code class="literal">IFD0</code> 区段中的该值。
  </p>
  <p class="para">
   <span class="function"><strong>exif_read_data()</strong></span> 还会根据 EXIF
   规范（<a href="http://exif.org/Exif2-2.PDF" class="link external">&raquo;&nbsp;http://exif.org/Exif2-2.PDF</a>，第
   20 页）来验证 EXIF 数据。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.exif-read-data-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">file</code></dt>

     <dd>

      <p class="para">
       图片文件的位置。这可以是一个文件路径（和往常一样，流封装也是支持的），
       或者是一个流式资源（stream <span class="type">resource</span>）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">required_sections</code></dt>

     <dd>

      <p class="para">
       是需要存在于文件中的逗号分隔的区段列表用来产生结果数组。如果未找到所请求的区段则返回值为
       <strong><code>false</code></strong>。
       <table class="doctable informaltable">
        
         <tbody class="tbody">
          <tr>
           <td>FILE</td>
           <td>FileName, FileSize, FileDateTime, SectionsFound</td>
          </tr>

          <tr>
           <td>COMPUTED</td>
           <td>
            html，Width，Height，IsColor，可能有更多其它的。Height
            和 Width 是用和 <span class="function"><a href="function.getimagesize.html" class="function">getimagesize()</a></span>
            一样的方法计算的，因此它们的值不能是任何返回的头信息的部分。此外
            <code class="literal">html</code> 是一个可以用于普通的 <abbr title="Hyper Text Markup Language">HTML</abbr> 中的 height/width 的文本字符串。
           </td>
          </tr>

          <tr>
           <td>ANY_TAG</td>
           <td>任何包含有标记的信息，例如 <code class="literal">IFD0</code>，<code class="literal">EXIF</code>，...</td>
          </tr>

          <tr>
           <td>IFD0</td>
           <td>
            所有 IFD0 的标记数据。在标准的图像文件中这包含了图像大小及其它。
           </td>
          </tr>

          <tr>
           <td>THUMBNAIL</td>
           <td>
            如果有第二个 <code class="literal">IFD</code>，文件应该包含有缩略图。所有有关嵌入缩略图的标记信息都存储在本区。
           </td>
          </tr>

          <tr>
           <td>COMMENT</td>
           <td>JPEG 图像的注释头信息。</td>
          </tr>

          <tr>
           <td>EXIF</td>
           <td>
            EXIF 区段是 <code class="literal">IFD0</code> 的子区，包含有图像的更多详细信息。大多数内容都是数码相机相关的。
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
    
     <dt>
<code class="parameter">as_arrays</code></dt>

      <dd>

      <p class="para">
       指定了是否每个区段都成为一个数组。<code class="parameter">required_sections</code>
       <code class="literal">COMPUTED</code>，<code class="literal">THUMBNAIL</code>
       和<code class="literal">COMMENT</code>
       区段总是成为数组，因为它们里面包含的名字和其它区段冲突。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">read_thumbnail</code></dt>

     <dd>

      <p class="para">
       当设定为 <strong><code>true</code></strong> 时，读取缩略图本身。否则只读取标记数据。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.exif-read-data-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个关联数组，键名是头信息名，值为与其相应的值。如果没有可供返回的数据，<span class="function"><strong>exif_read_data()</strong></span>
   将返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.exif-read-data-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   当遇到不支持的标签，或其他潜在的错误情况时，将抛出<strong><code>E_WARNING</code></strong>
   与<strong><code>E_NOTICE</code></strong>等级的错误，但这个函数依然会尝试去读取所有可理解的信息。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.exif-read-data-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        <code class="parameter">required_sections</code> 现在可以为空。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <code class="parameter">file</code> 参数现在起支持本地文件和流式资源。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        新增了以下 EXIF 格式的支持：
        <ul class="itemizedlist">
         <li class="listitem">
          <span class="simpara">
           Samsung
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           DJI
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           Panasonic
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           Sony
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           Pentax
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           Minolta
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           Sigma/Foveon
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           AGFA
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           Kyocera
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           Ricoh
          </span>
         </li>
         <li class="listitem">
          <span class="simpara">
           Epson
          </span>
         </li>
        </ul>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.exif-read-data-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2604">
    <p><strong>示例 #1 <span class="function"><strong>exif_read_data()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"test1.jpg:&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$exif&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">exif_read_data</span><span style="color: #007700">(</span><span style="color: #DD0000">'tests/test1.jpg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'IFD0'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$exif</span><span style="color: #007700">===</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">"No&nbsp;header&nbsp;data&nbsp;found.&lt;br&nbsp;/&gt;\n"&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">"Image&nbsp;contains&nbsp;headers&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$exif&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">exif_read_data</span><span style="color: #007700">(</span><span style="color: #DD0000">'tests/test2.jpg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"test2.jpg:&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$exif&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$section</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$section&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$key</span><span style="color: #DD0000">.</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     第一个调用失败了，因为图像没有头信息。
    </p></div>
    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
test1.jpg:
No header data found.
test2.jpg:
FILE.FileName: test2.jpg
FILE.FileDateTime: 1017666176
FILE.FileSize: 1240
FILE.FileType: 2
FILE.SectionsFound: ANY_TAG, IFD0, THUMBNAIL, COMMENT
COMPUTED.html: width=&quot;1&quot; height=&quot;1&quot;
COMPUTED.Height: 1
COMPUTED.Width: 1
COMPUTED.IsColor: 1
COMPUTED.ByteOrderMotorola: 1
COMPUTED.UserComment: Exif test image.
COMPUTED.UserCommentEncoding: ASCII
COMPUTED.Copyright: Photo (c) M.Boerger, Edited by M.Boerger.
COMPUTED.Copyright.Photographer: Photo (c) M.Boerger
COMPUTED.Copyright.Editor: Edited by M.Boerger.
IFD0.Copyright: Photo (c) M.Boerger
IFD0.UserComment: ASCII
THUMBNAIL.JPEGInterchangeFormat: 134
THUMBNAIL.JPEGInterchangeFormatLength: 523
COMMENT.0: Comment #1.
COMMENT.1: Comment #2.
COMMENT.2: Comment #3end
THUMBNAIL.JPEGInterchangeFormat: 134
THUMBNAIL.Thumbnail.Height: 1
THUMBNAIL.Thumbnail.Height: 1
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2605">
    <p><strong>示例 #2 <span class="function"><strong>exif_read_data()</strong></span> with streams available as of PHP 7.2.0</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Open&nbsp;a&nbsp;the&nbsp;file,&nbsp;this&nbsp;should&nbsp;be&nbsp;in&nbsp;binary&nbsp;mode<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/image.jpg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rb'</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Error:&nbsp;Unable&nbsp;to&nbsp;open&nbsp;image&nbsp;for&nbsp;reading'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Attempt&nbsp;to&nbsp;read&nbsp;the&nbsp;exif&nbsp;headers<br /></span><span style="color: #0000BB">$headers&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">exif_read_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$headers</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Error:&nbsp;Unable&nbsp;to&nbsp;read&nbsp;exif&nbsp;headers'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Print&nbsp;the&nbsp;'COMPUTED'&nbsp;headers<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'EXIF&nbsp;Headers:'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$headers</span><span style="color: #007700">[</span><span style="color: #DD0000">'COMPUTED'</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$header&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">'&nbsp;%s&nbsp;=&gt;&nbsp;%s%s'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$header</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
EXIF Headers:
 Height =&gt; 576
 Width =&gt; 1024
 IsColor =&gt; 1
 ByteOrderMotorola =&gt; 0
 ApertureFNumber =&gt; f/5.6
 UserComment =&gt;
 UserCommentEncoding =&gt; UNDEFINED
 Copyright =&gt; Denis
 Thumbnail.FileType =&gt; 2
 Thumbnail.MimeType =&gt; image/jpeg
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.exif-read-data-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    If <a href="ref.mbstring.html" class="link">mbstring</a> is enabled, exif will attempt to process
    the unicode and pick a charset as specified by
    <a href="exif.configuration.html#ini.exif.decode-unicode-motorola" class="link">exif.decode_unicode_motorola</a> and
    <a href="exif.configuration.html#ini.exif.decode-unicode-intel" class="link">exif.decode_unicode_intel</a>. The exif
    extension will not attempt to figure out the encoding on its own, and it is up to the user
    to properly specify the encoding for which to use for decoding by setting one of these two
    ini directives prior to calling <span class="function"><strong>exif_read_data()</strong></span>.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    If the <code class="parameter">file</code> is used to pass a stream to this function, then the stream
    must be seekable. Note that the file pointer position is not changed after this function returns.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.exif-read-data-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.exif-thumbnail.html" class="function" rel="rdfs-seeAlso">exif_thumbnail()</a> - 检索图像的嵌入式缩略图</span></li>
    <li class="member"><span class="function"><a href="function.getimagesize.html" class="function" rel="rdfs-seeAlso">getimagesize()</a> - 取得图像大小</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>